extern "C" {
#include "lua5.1/lua.h"
#include "lua5.1/lualib.h"
#include "lua5.1/lauxlib.h"
}
#include <vector>
#include "fitsio.h"


class Image {
 public:
  Image ();
  ~Image ();
  float *data;
  unsigned int sizex, sizey;
  float xref, yref, pixelscale;
  char *name;
  int sminx, smaxx, sminy, smaxy;
  float flmax,flmin,xref1,yref1;
};

class Lua
{ 
 public:
  Lua (const char * = NULL);
  ~Lua ();

  bool ExecFile (const char *file);
  bool ExecString (const char *str);

  // Callable from lua scripts
  static int Rectangle     (lua_State *);
  static int FilledRectangle     (lua_State *);
  static int Ellipse       (lua_State *L);
  static int DrawLine      (lua_State *L);
  static int DrawCone      (lua_State *L);
  static int DrawArrow     (lua_State *L);
  static int Text          (lua_State *L);
  static int DrawScaleBar  (lua_State *L);
  static int DrawCross     (lua_State *L);

  static int DrawSlit      (lua_State *L);
  static int DrawRegion    (lua_State *L);
  static int Contour       (lua_State *L);

  static int TextSize      (lua_State *L);
  static int LineWidth     (lua_State *L);
  static int SetColor      (lua_State *L);
  static int LineStyle  (lua_State *L);
  static int SetContourSampling  (lua_State *L);
  static int SetContourSmoothing  (lua_State *L);
  static int SetContourSaving  (lua_State *L);
  static int SetFillStyle(lua_State*);

  static int Input         (lua_State *L);

  static int LoadImage     (lua_State *L);
  static int FreeImage     (lua_State *L);
  static int SaveFits      (lua_State *L);

  static int PxReplace     (lua_State *L);
  static int PxArith       (lua_State *L);

  static int SubSample      (lua_State *L);
  static int SmoothGaussian (lua_State *L);
  static int ClipImage (lua_State *L);

  static int ListCommands (lua_State *L);

  static int ViewPort (lua_State *L);
  static int Limits (lua_State *L);
  static int Box (lua_State *L);
  static int Plot (lua_State *L);

 private:
  lua_State* L;

  //static bool CheckArgsNumeric (lua_State *,int);

  static int MethodError (lua_State *, const char *);
  static int Success (lua_State *);
  static int FindImage (const char *);

  static int DrawArrow4  (lua_State *L);
  static int DrawArrow7  (lua_State *L);

  class CheckArgsNumeric  {
  public:
    CheckArgsNumeric (lua_State *,int);
    ~CheckArgsNumeric ();
    
    float *val;
    
    bool Success (void) {return success;};
    
  private:
    CheckArgsNumeric (); // Not implemented
    bool success;
  };

  class CheckArgsNumericAlpha  {
  public:
    CheckArgsNumericAlpha (lua_State *,int, int);
    ~CheckArgsNumericAlpha ();
    
    float *val;
    char const **  text;
    
    bool Success (void) {return success;};
    
  private:
    CheckArgsNumericAlpha (); // Not implemented
    bool success;
  };

  class CheckArgsAlphaNumeric  {
  public:
    CheckArgsAlphaNumeric (lua_State *,int, int);
    ~CheckArgsAlphaNumeric ();
    
    float *val;
    char const **  text;
    
    bool Success (void) {return success;};
    
  private:
    CheckArgsAlphaNumeric (); // Not implemented
    bool success;
  };

  static int LoadFitsImage(fitsfile*, Image*);
  static int LoadDatImage (const char*, Image*);

};

